﻿//-----------------------------------------------------------------------
// <copyright file="ToolStripColorPicker.cs" company="LMT">
//     Copyright (c) by La Minh Thuận.
// </copyright>
// <author>thuanla1985@hotmail.com</author>
//-----------------------------------------------------------------------
namespace LMT.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// TooStrip color picker tool
    /// </summary>
    public partial class ToolStripColorPicker : ToolStripSplitButton
    {
        /// <summary>
        /// WM USER value
        /// </summary>
        private const int WmUser = 0x0400;

        /// <summary>
        /// WM REFLECT value
        /// </summary>
        private const int WmReflect = WmUser + 0x1C00;

        /// <summary>
        /// WM COMMAND value
        /// </summary>
        private const int WmCommand = 0x0111;

        /// <summary>
        /// CBN DROPDOWN value
        /// </summary>
        private const int CbnDropDown = 7;

        /// <summary>
        /// instance of OfficeMenuHelper
        /// </summary>
        private OfficeMenuHelper clrPlate;

        /// <summary>
        /// the boundary for showing color
        /// </summary>
        private Rectangle clrRect = Rectangle.Empty;

        /// <summary>
        /// the selected brush color
        /// </summary>
        private Brush selectedBrush;

        /// <summary>
        /// Initializes a new instance of the <see cref="ToolStripColorPicker"/> class.
        /// </summary>
        public ToolStripColorPicker()
        {
            this.Initialize();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ToolStripColorPicker"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        public ToolStripColorPicker(IContainer container) : this()
        {
            container.Add(this);
        }

        /// <summary>
        /// Occurs when [selected color changed].
        /// </summary>
        [Browsable(true)]
        public event EventHandler SelectedColorChanged;

        /// <summary>
        /// Gets or sets the color.
        /// </summary>
        /// <value>The color.</value>
        [Browsable(true)]
        public Color Color
        {
            get { return this.clrPlate.ColorPalette.SelectedColor; }
            set { this.clrPlate.ColorPalette.SelectedColor = value; }
        }

        /// <summary>
        /// Gets or sets the size of the item.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// A <see cref="T:System.Drawing.Size"/>, representing the width and height of a rectangle.
        /// </returns>
        /// <PermissionSet>
        ///     <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        ///     <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        ///     <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
        ///     <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// </PermissionSet>
        public override Size Size
        {
            get
            {
                return base.Size;
            }

            set
            {
                base.Size = value;
                this.clrRect.Y = value.Height - 5;
                this.clrRect.Width = value.Width - this.ButtonBounds.Width - (2 * this.clrRect.Left);
            }
        }

        /// <summary>
        /// HIs the word.
        /// </summary>
        /// <param name="n">The low word integer value.</param>
        /// <returns>integer value</returns>
        public static int HIWord(int integer)
        {
            return (integer >> 16) & 0xffff;
        }        
        
        /// <summary>
        /// Raised in response to the <see cref="M:System.Windows.Forms.ToolStripDropDownItem.ShowDropDown"/> method.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnDropDownShow(EventArgs e)
        {
            base.OnDropDownShow(e);            
            this.ShowPopUp();                               
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.FillRectangle(this.selectedBrush, this.clrRect);
        }

        /// <summary>
        /// Handles the SelectedColorChanged event of the colorPlate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ColorPlateSelectedColorChanged(object sender, EventArgs e)
        {
            if (this.selectedBrush != null)
            {
                this.selectedBrush.Dispose();
                this.selectedBrush = null;
            }

            this.selectedBrush = new SolidBrush(this.clrPlate.ColorPalette.SelectedColor);
            this.Invalidate();

            if (this.SelectedColorChanged != null)
            {
                this.SelectedColorChanged(this, new EventArgs());
            }
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize()
        {
            this.clrPlate = new OfficeMenuHelper();
            this.clrPlate.ColorPalette.SelectedColorChanged += new EventHandler(this.ColorPlateSelectedColorChanged);

            this.selectedBrush = new SolidBrush(this.clrPlate.ColorPalette.SelectedColor);
            this.clrRect = new Rectangle(2, 2, 2, 5);
        }

        /// <summary>
        /// Shows the pop up.
        /// </summary>
        private void ShowPopUp()
        {
            if (this.clrPlate != null)
            {
                // Get resolution of screen
                int maxWidth = SystemInformation.WorkingArea.Width;
                int maxHeight = SystemInformation.WorkingArea.Height;

                // Location Click
                Point location = System.Windows.Forms.Cursor.Position;

                location.X -= this.Width;
                location.Y += (int)this.Height / 2;
                
                if (location.Y + this.clrPlate.Height > maxHeight)
                {
                    location.Y -= this.clrPlate.Height + this.Height;
                }        

                if (location.X + this.clrPlate.Width > maxWidth)
                {
                    location.X -= this.clrPlate.Width - this.Width;
                }

                this.clrPlate.Location = location;
                this.clrPlate.Show();
            }
        }
    }
}